/**
 * 弹出框JS文件
 * by 杨婷
 */

function alertMsg(msg, mode,web_title,h,myHeight) { //mode为空，即只有一个确认按钮，mode为1时有确认和取消两个按钮
        msg = msg || '';
        mode = mode || 0;
    h=h;
        var top = document.body.scrollTop || document.documentElement.scrollTop;
        var isIe = (document.all) ? true : false;
        var isIE6 = isIe && !window.XMLHttpRequest;
        var sTop = document.documentElement.scrollTop || document.body.scrollTop;
        var sLeft = document.documentElement.scrollLeft || document.body.scrollLeft;
        var winSize = function(){
            var xScroll, yScroll, windowWidth, windowHeight, pageWidth, pageHeight;
            // innerHeight获取的是可视窗口的高度，IE不支持此属性
            if (window.innerHeight && window.scrollMaxY) {
                xScroll = document.body.scrollWidth;
                yScroll = window.innerHeight + window.scrollMaxY;
            } else if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
                xScroll = document.body.scrollWidth;
                yScroll = document.body.scrollHeight;
            } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
                xScroll = document.body.offsetWidth;
                yScroll = document.body.offsetHeight;
            }

            if (self.innerHeight) {    // all except Explorer
                windowWidth = self.innerWidth;
                windowHeight = self.innerHeight;
            } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
                windowWidth = document.documentElement.clientWidth;
                windowHeight = document.documentElement.clientHeight;
            } else if (document.body) { // other Explorers
                windowWidth = document.body.clientWidth;
                windowHeight = document.body.clientHeight;
            }

            // for small pages with total height less then height of the viewport
            if (yScroll < windowHeight) {
                pageHeight = windowHeight;
            } else {
                pageHeight = yScroll;
            }

            // for small pages with total width less then width of the viewport
            if (xScroll < windowWidth) {
                pageWidth = windowWidth;
            } else {
                pageWidth = xScroll;
            }

            return{
                'pageWidth':pageWidth,
                'pageHeight':pageHeight,
                'windowWidth':windowWidth,
                'windowHeight':windowHeight
            }
        }();
        //alert(winSize.pageWidth);
        //遮罩层
        var styleStr = 'top:0;left:0;position:absolute;z-index:10000;background:#666;width:' + winSize.pageWidth + 'px;height:' +  (winSize.pageHeight + 30) + 'px;';
        styleStr += (isIe) ? "filter:alpha(opacity=80);" : "opacity:0.8;"; //遮罩层DIV
        var shadowDiv = document.createElement('div'); //添加阴影DIV
        shadowDiv.style.cssText = styleStr; //添加样式
        shadowDiv.id = "shadowDiv";
        //如果是IE6则创建IFRAME遮罩SELECT
        if (isIE6) {
            var maskIframe = document.createElement('iframe');
            maskIframe.style.cssText = 'width:' + winSize.pageWidth + 'px;height:' + (winSize.pageHeight + 30) + 'px;position:absolute;visibility:inherit;z-index:-1;filter:alpha(opacity=0);';
            maskIframe.frameborder = 0;
            maskIframe.src = "about:blank";
            shadowDiv.appendChild(maskIframe);
        }
        document.body.insertBefore(shadowDiv, document.body.firstChild); //遮罩层加入文档
        //弹出框
        var styleStr1 = 'display:block;position:fixed;_position:absolute;left:' + (winSize.windowWidth / 2 - 450) + 'px;top:' + (winSize.windowHeight / 2 - myHeight) + 'px;_top:' + (winSize.windowHeight / 2 + top - 150)+ 'px;'; //弹出框的位置
        var alertBox = document.createElement('div');
        alertBox.id = 'alertMsg';
        alertBox.style.cssText = styleStr1;
    var alertMsg_title=document.createElement('div');
    alertMsg_title.id='alertMsg_title';
    alertMsg_title.innerHTML=web_title;
    var alertMsg_close=document.createElement('i');
    alertMsg_close.id="alertMsg_close";
    alertMsg_close.onclick = function () {
                document.body.removeChild(alertBox);
                document.body.removeChild(shadowDiv);
                return false;
        };
        alertMsg_title.appendChild(alertMsg_close);
    alertBox.appendChild(alertMsg_title);
        //创建弹出框里面的内容P标签
        var alertMsg_info = document.createElement('div');
        alertMsg_info.id = 'alertMsg_info';
        alertMsg_info.innerHTML = msg;
        alertBox.appendChild(alertMsg_info);
        //创建按钮
        var btn1 = document.createElement('a');
        btn1.id = 'alertMsg_btn1';
        if(mode!=2){
              if(h==''){
            btn1.href='javas' + 'cript:void(0)';
            btn1.innerHTML = '<cite>确定</cite>';
            btn1.onclick=function(){
                  document.body.removeChild(alertBox);
                  document.body.removeChild(shadowDiv);
            history.back();
            return true;
            };
          }
          else
              {
            btn1.href = h;
            btn1.innerHTML = '<cite>确定</cite>';
            btn1.onclick = function () {
                  document.body.removeChild(alertBox);
                  document.body.removeChild(shadowDiv);
                  return true;
                };
          }
              alertBox.appendChild(btn1);
              if (mode === 1) {
                  var btn2 = document.createElement('a');
                  btn2.id = 'alertMsg_btn2';
                  btn2.href = 'javas' + 'cript:void(0)';
                  btn2.innerHTML = '<cite>取消</cite>';
                  btn2.onclick = function () {
                      document.body.removeChild(alertBox);
                      document.body.removeChild(shadowDiv);
                      return false;
                  };
                  alertBox.appendChild(btn2);
              }
        }

        document.body.appendChild(alertBox);

}

$(function(){
    //$(".g-d-span").click(function(){
    //    alert('a');
    //});
    $(".cr-content").delegate(".goods-img","click",function(){
        $(".goods-detail-con").hide();
        $(this).parents(".goods-con").find(".goods-detail-con").show();
    });
    $(".cr-content").delegate(".glClose","click",function(){
        $element=$(this).parents(".goods-detail-con");
        $element.hide();
        //console.log($(this).parents(".goods-detail-con").html());
        //$(this).parents(".goods-detail-con").hide();
    });

    $(".cr-content").delegate(".glDelete","click",function(){
        removeGoodItems($(this), $("#tel").val(), $("#type").val());
    });
    $(".cr-content").delegate(".g-d-span-delete","click",function(){
        removeGoodItems($(this), $("#tel").val(), $("#type").val());
    });
    $(".cr-content").delegate(".glModify","click",function(){
        editGoodItems($(this));
    });
    $(".cr-content").delegate(".g-d-span-edit","click",function(){
        editGoodItems($(this));
    });
});

function removeGoodItems(thisItem, tel, type) {
    if(confirm("删除是不可恢复的，你确认要删除吗？")){
        delete_ajax(thisItem.parents(".goods-detail-con").find(".good-md5").attr("value"), tel);
        thisItem.parents(".goods-con").remove();
    }
}

function editGoodItems(thisItem) {
    //alert(thisItem.parents(".goods-detail-con").find(".good-md5").attr("value"));
    if(confirm("你确认要编辑当前素材吗？")){
        window.location.href='/AppWebBgImgEditIndex?md5=' + thisItem.parents(".goods-detail-con").find(".good-md5").attr("value");
    }
}

function delete_ajax(md5, tel){
    jQuery.ajax({
        url     : 'AppWebBgImgDelete',
        type    : 'POST',
        dataType: 'json',
        data    :{
            'md5': md5,
            'tel': tel,
        },
        success : function(data){

        }
    });
}
